﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RuEn
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
    public class LengthRule : RuleBase
    {
        private string _errorMessage = "Input is of in-correct Length";
        private int _maxLength = 100;
        private int _minLength = 0;

        public LengthRule(int MaxLength, int MinLength)
        {
            _maxLength = MaxLength;
            _minLength = MinLength;
            _errorMessage = ErrorMessage;
        }
        public LengthRule(int MaxLength, int MinLength, string ErrorMessage)
        {
            _maxLength = MaxLength;
            _minLength = MinLength;
            _errorMessage = ErrorMessage;
        }

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        public int MaxLength
        {
            get { return _maxLength; }
            set { _maxLength = value; }
        }

        public int MinLength
        {
            get { return _minLength; }
            set { _minLength = value; }
        }

        public bool Validate(string value, int MaxLength, int MinLength, string ErrorMessage)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Trim().Length < MinLength || value.Trim().Length > MaxLength)
                {
                    BrokenRules.Add(ErrorMessage);
                    return false;
                }
            }
            return true;
        }
    }
}
